package api

import (
	"context"
	"strconv"

	"gitee.com/go-micro-services/api/utils"
	cart "gitee.com/go-micro-services/cart/proto/cart"
	"github.com/gin-gonic/gin"
)

type ApiController struct{}

func (con ApiController) FindAll(c *gin.Context) {
	// 1. 获取参数
	user_id_str := c.Query("user_id")
	userId, err := strconv.ParseInt(user_id_str, 10, 64)
	if err != nil {
		c.JSON(200, gin.H{
			"message": "参数异常",
			"success": false,
		})
		return
	}
	// 2. rpc 远程调用：获取购物车所有商品
	cartClient := cart.NewCartService(utils.CartServices, utils.SrvClient)
	cartAll, err := cartClient.GetAll(context.TODO(), &cart.CartFindAll{UserId: userId})
	c.JSON(200, gin.H{
		"data":    cartAll,
		"success": true,
	})
}
